﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StuManagementSystem
{
    public partial class frm_ChangePwd : Form
    {
        DbContext db = new DbContext();
        private int ChangeUserId;
        private string UserType;
        public frm_ChangePwd()
        {
            InitializeComponent();
        }
        public frm_ChangePwd(int Id,string Type)
        {
            InitializeComponent();
            this.ChangeUserId = Id;
            this.UserType = Type;
        }

        private void frm_CPwd_Load(object sender, EventArgs e)
        {
            txt_OldPwd.Focus();
        }

        #region 返回
        /// <summary>
        /// 返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_back_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
        

        private void btn_Change_Click(object sender, EventArgs e)
        {
            if (!CheckIsNull())
                return;
            #region 获取数据
            var OldPwd = this.txt_OldPwd.Text;
            var NewPwd = this.txt_NewPwd.Text;
            var NewPwds = this.txt_NewPwds.Text;
            string sql = "";
            if (UserType.Equals("Manager"))
                sql = string.Format(@"select LoginPwd from Admin where AdminId = {0}", ChangeUserId);
            else
                sql = string.Format(@"select StuPwd from StuInfo where StuCode = {0}", ChangeUserId);
            if (!OldPwd.Equals(db.ExecScaler(sql).ToString()))
            {
                MessageBox.Show("原密码错误，请重新输入！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txt_OldPwd.Focus();
                return;
            }
            if (UserType.Equals("Manager"))
                sql = String.Format(@"UPDATE dbo.Admin SET LoginPwd = {0} WHERE AdminId = {1}", NewPwd, ChangeUserId);
            else
                sql = String.Format(@"UPDATE dbo.StuInfo SET StuPwd = {0} WHERE StuCode = {1}", NewPwd, ChangeUserId);
            if (db.ExecNonQuery(sql))
            {
                MessageBox.Show("更改成功！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (UserType.Equals("Manager"))
                {
                    frm_ManagerIndex frm1 = (frm_ManagerIndex)this.MdiParent;
                    frm1.mns_AccountChange_Click(null, null);
                }
                else
                {
                    frm_StuIndex frm1 = (frm_StuIndex)this.MdiParent;
                    frm1.mns_AccountChange_Click(null, null);
                }
                    
                
                
                this.Close();
            }
            else
                MessageBox.Show("更改失败！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            #endregion

        }

        private bool CheckIsNull()
        {
            if (string.IsNullOrWhiteSpace(this.txt_OldPwd.Text))
            {
                MessageBox.Show("旧密码不能为空");
                this.txt_OldPwd.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(this.txt_NewPwd.Text))
            {
                MessageBox.Show("新密码不能为空");
                this.txt_NewPwd.Focus();
                return false;
            }
            if (string.IsNullOrWhiteSpace(this.txt_NewPwds.Text))
            {
                MessageBox.Show("确认密码不能为空");
                this.txt_NewPwds.Focus();
                return false;
            }
            if (!this.txt_NewPwd.Text.Equals(this.txt_NewPwds.Text))
            {
                MessageBox.Show("两次密码不一致");
                this.txt_NewPwds.Focus();
                return false;
            }
            return true;
        }

    }
}
